package com.toolset.Activity.ToolItemActivity;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.toolset.Activity.BaseActivity;
import com.toolset.R;
import com.toolset.Util.HttpUtil;

import java.util.List;
import java.util.concurrent.CountDownLatch;

import okhttp3.FormBody;

/*
翻译工具 需要网络
 */
/*
Android中网络编程注意事项
    1：网络请求不能再主线程中执行 防止阻塞
    2：网络请求子线程中执行 不能直接把结果写到View中  需要主线程来操作
    3：需要在AndroidManifest.xml中申请网络权限  加入<uses-permission android:name="android.permission.INTERNET"/>
 */

public class Translation extends BaseActivity {

    private EditText translationInputEdit;
    private EditText translationOutputEdit;
    private Button translationBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translation);

        //初始化事件
        initEven();

        //设置点击翻译按钮
        translationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取输入文本信息
                final String text = translationInputEdit.getText().toString();

                final StringBuffer sb = new StringBuffer();


                //当输入文本长度不为0时
                if(text.length()!=0){

                    //创建打开对话框
                    final ProgressDialog dialog = new ProgressDialog(Translation.this);
                    dialog.setTitle("正在翻译文本");
                    dialog.setMessage("翻译中，请稍等~");
                    dialog.setCancelable(true);
                    dialog.show();


                    //创建一个新线程
                    Thread thread = new Thread(){
                        public void run(){
                            //设置Post数据
                            FormBody body = new FormBody.Builder()
                                    .add("q",text)
                                    .add("from","Auto")
                                    .add("to","Auto")
                                    .build();

                            //发送数据获取返回结果
                            String result = HttpUtil.Post("https://aidemo.youdao.com/trans",body);

                            //结果解析为JsonObject对象
                            JSONObject jsonObject = JSON.parseObject(result);

                            //获取到key为 translation 的结果
                            List<String> resultList= (List<String>) jsonObject.get("translation");

                            //将结果转化为字符串
                            for (int i =0;i<resultList.size();i++)
                                sb.append(resultList.get(i)+"\n");

                            //切换主线程更新视图数据
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //写入输出框
                                    translationOutputEdit.setText(sb.toString());
                                    //关闭对话框
                                    dialog.dismiss();
                                }
                            });

                        }

                    };
                    //启动线程
                    thread.start();



                }//当输入文本长度为0时
                else {
                    Toast.makeText(Translation.this,"请先输入翻译文本再点击翻译！",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //初始化事件
    private void initEven() {
        translationInputEdit = (EditText) findViewById(R.id.TranslationInputEdit);
        translationOutputEdit = (EditText) findViewById(R.id.TranslationOuputEdit);
        translationBtn = (Button) findViewById(R.id.TranslationBtn);
    }
}
